<?php
namespace Admin\Controller;
use Think\Controller;
class LoginController extends Controller {
	public function login(){
		
		$this -> display('');
	}

	public function doLogin(){
		//根据输入用户名找到数据库该条数据
		$data['user_name'] = I('post.username');
		$res_admin = M('admin_user') -> where($data) -> find();	
		
		//如果找到			
			if($res_admin){
				if(empty($res_admin['ec_salt'])){
						$password = md5(I('post.password'));
						if($res_admin['password']==$password){
							$_SESSION['admin'] = $res_admin;						
							$this -> success('登陆成功',U('admin/role/index'));
						}else{
							$this -> success('登陆失败,您的密码错误',U('admin/login/login'));
						}
					}else{
						$password = md5(md5(I('post.password')).$res_admin['ec_salt']);
						if($res_admin['password']==$password){
							$_SESSION['admin'] = $res_admin;
							$admin_role_mod = M('admin_role');							
							//搜索登录管理员的所有权限							
								$admin_role = $admin_role_mod->where('role = "%s" ',$res_admin['role'])->find();
								$node_id = explode(',',$admin_role['node_id']);
							//判断管理员是否拥有该功能,并且将其封装进数组
								$node_role = M('admin_node');	
								$mail['role_id'] = array('IN',$node_id);						
								$admin_node = $node_role ->where($mail) ->select() ;
								foreach($admin_node as $v){
									$node_con[] = $v['role_controller_name'];
									$node_act[] = $v['role_action_name'];
									if($v['role_action_name']=='add'){
										$node_act[] = 'insert';
									}
									if($v['role_action_name']=='add_role'){
										$node_act[] = 'insert_role';
									}
									if($v['role_action_name']=='edit'){
										$node_act[] = 'update';
									}
									if($v['role_action_name']=='set'){
										$node_act[] = 'edit1';
										$node_act[] = 'edit2';
									}
									if($v['role_action_name']=='jibie'){
										$node_act[] = 'shengjihuiyuan';
										$node_act[] = 'eidt';
										$node_act[] = 'edit1';
										$node_act[] = 'shengji';
										
									}
									if($v['role_action_name']=='ppejf'){
										$node_act[] = 'actejfid';
										$node_act[] = 'actejfrs';
									}
									if($v['role_action_name']=='pprg'){
										$node_act[] = 'actrgjf';										
									}
									if($v['role_action_name']=='bianji'){
										$node_act[] = 'xiugai';										
										$node_act[] = 'baodan';										
										$node_act[] = 'dongjie';										
										$node_act[] = 'jiedong';										
									}
									if($v['role_action_name']=='tiaojie'){
										$node_act[] = 'update';										
										$node_act[] = 'baodan';										
									}
									if($v['role_action_name']=='edit_role'){
										$node_act[] = 'update_role';																		
									}
								}
								$node_con[] = 'layout';
								$node_act[] = 'index';
								$_SESSION['node_con'] = $node_con;
								$_SESSION['node_act'] = $node_act;
							$this -> success('登陆成功',U('admin/index/index'));
						}else{
							$this -> error('登陆失败,您的密码错误',U('admin/login/login'));
						}		
					}
			}else{
				$this -> error('该用户名不存在',U('admin/login/login'));	
			}       
	
	
	}
	//退出登录
	public function loginout(){
		session('[destroy]'); 	
		$this->redirect('/admin/login/login');
	}




}